package com.yunhoo.base.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.base.model.Invent;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class InventImpl {

    private static final String NAMESPACE = "invent.";

    private static Map newMap() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        return map;
    }

    public static List<Invent> list(String inventTypeCode) {
        Map map = newMap();
        map.put("type", inventTypeCode);
        return IbatisTemplate.queryForList(NAMESPACE + "list", map);
    }

    public static boolean insert(Invent data) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            /* 设置编号 */
            data.setId(maxNumber(sqlmap));

            sqlmap.insert(NAMESPACE + "insert", data);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(InventImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(InventImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return true;
    }

    private static String maxNumber(SqlMapClient sqlmap) throws SQLException {
        Integer maxNumber = (Integer) sqlmap.queryForObject(NAMESPACE + "maxCode", LoginParam.getOrg().getCode());
        return String.format("%06d%n", (maxNumber == null ? 0 : maxNumber) + 1).trim();
    }

    public static boolean update(Invent data) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            sqlmap.update(NAMESPACE + "update", data);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(InventImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(InventImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return true;
    }

    public static boolean delete(Invent data) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            sqlmap.delete(NAMESPACE + "delete", data);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(InventImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(InventImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return true;
    }

    public static void main(String[] args) {
    }
}
